package com.linln.modules.system.service.impl;

import com.linln.common.data.QuerySpec;
import com.linln.common.utils.DateUtils;
import com.linln.modules.system.domain.OrderPorters;
import com.linln.modules.system.repository.OrderPortersRepository;
import com.linln.modules.system.service.OrderPortersService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 通义灵码
 * @date 2023/10/10
 */
@Service
public class OrderPortersServiceImpl implements OrderPortersService {

    @Autowired
    private OrderPortersRepository orderPortersRepository;

    /**
     * 更新订单护工记录
     * @param orderId 订单id
     * @param befPorterId  前一个护工id
     * @param aftPorterId  更换后护工id
     * @param companyId     公司id
     * @param serviceStatus  服务状态
     * @param timeStar      开始时间
     * @param timeEnd           结束时间
     * @param type          更换方式
     * @param orderType     订单类型
     */
    @Override
    public void updatePorterOrder(Long orderId, Long befPorterId, Long aftPorterId, Long companyId, Integer serviceStatus, String timeStar, String timeEnd, Integer type, Integer orderType) {
        //判断更换方式 1默认2患者申请更换3公司更换
        if (type==1){
            OrderPorters tempPorters = new OrderPorters();
            tempPorters.setOrderId(orderId);
            tempPorters.setPorterId(befPorterId);
            Example<OrderPorters> portersExample = Example.of(tempPorters);
            List<OrderPorters> orderPortersList = orderPortersRepository.findAll(portersExample);
            //判断该护工之前是否有记录
            if (orderPortersList.size()>0){
                OrderPorters orderPorters = orderPortersList.get(0);
                orderPorters.setServiceStatus(serviceStatus);
                if (ObjectUtils.isNotEmpty(timeStar)){
                    orderPorters.setTimeStar(timeStar);
                }
                orderPorters.setTimeEnd(timeEnd);
                orderPorters.setStatus((byte) 1);
                orderPortersRepository.save(orderPorters);
            }else{
                OrderPorters orderPorters = new OrderPorters();
                orderPorters.setOrderId(orderId);
                orderPorters.setCompanyId(companyId);
                orderPorters.setPorterId(befPorterId);
                orderPorters.setServiceStatus(serviceStatus);
                orderPorters.setTimeStar(timeStar);
                orderPorters.setTimeEnd(timeEnd);
                orderPorters.setType(type);
                orderPorters.setOrderType(orderType);
                orderPorters.setStatus((byte) 1);
                orderPortersRepository.save(orderPorters);
            }
        }else{
            OrderPorters tempPorters = new OrderPorters();
            tempPorters.setOrderId(orderId);
            tempPorters.setPorterId(befPorterId);
            Example<OrderPorters> portersExample = Example.of(tempPorters);
            List<OrderPorters> orderPortersList = orderPortersRepository.findAll(portersExample);
            if (orderPortersList.size()>0){
                String date = DateUtils.getDates();
                OrderPorters orderPorters = orderPortersList.get(0);
                //更新前一个护工的信息
                if (serviceStatus==0){
                    orderPorters.setServiceStatus(3);
                } else if (serviceStatus==1) {
                    orderPorters.setServiceStatus(2);
                }
                orderPorters.setType(type);
                orderPorters.setTimeEnd(date);
                orderPorters.setStatus((byte) 1);
                orderPortersRepository.save(orderPorters);
                //更新后一个护工的信息
                if (ObjectUtils.isNotEmpty(aftPorterId)){
                    OrderPorters orderPortera = new OrderPorters();
                    orderPortera.setOrderId(orderId);
                    orderPortera.setCompanyId(companyId);
                    orderPortera.setPorterId(aftPorterId);
                    orderPortera.setServiceStatus(serviceStatus);
                    orderPortera.setTimeStar(date);
                    orderPortera.setTimeEnd(timeEnd);
                    orderPortera.setType(type);
                    orderPortera.setOrderType(orderType);
                    orderPortera.setStatus((byte) 1);
                    orderPortersRepository.save(orderPortera);
                }
            }
        }
    }
}
